package com.shsnc.agent.service;

import java.util.Date;
import java.util.Map;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import com.shsnc.agent.bean.BaseLogInfoModel;
import com.shsnc.agent.bean.LogDetailModel;
import com.shsnc.agent.bean.OperInfo;
import com.shsnc.agent.enums.OperType;
import com.shsnc.agent.util.FiledInfoUtil;
import com.shsnc.agent.util.SendUtil;
import com.shsnc.agent.util.TemplateTranslateProcessor;

/**
 *
 * @author zll
 * @date 2021/8/5
 */
public class LogHandle {

	private TemplateTranslateProcessor translateProcessor=new TemplateTranslateProcessor();

	public void deal(BaseLogInfoModel baseLogInfo, LogDetailModel logDetail){
		OperInfo operInfo=new OperInfo();
		operInfo.setOperTime(DateUtil.format(new Date(baseLogInfo.getRequestTime()), DatePattern.UTC_SIMPLE_PATTERN));
		operInfo.setSessionId(String.valueOf(baseLogInfo.getLogId()));
		operInfo.setOperAcct(baseLogInfo.getUsername());
		operInfo.setOperObj(baseLogInfo.getUsername());
		operInfo.setSrcIp(baseLogInfo.getRequestIp());
		//目的IP
		operInfo.setDestIp("10.142.253.4");
		//目的端口
		operInfo.setDestPort("8080");

		operInfo.setOperType(transformationOperType(baseLogInfo.getRequestType()));
		operInfo.setOperSubType(transformationOperSubType(baseLogInfo.getRequestType()));

		operInfo.setOper(baseLogInfo.getDescription());
		operInfo.setOperDesc(baseLogInfo.getDescription());
		operInfo.setOperResult("0");
		operInfo.setFromSys("应用系统");

		String resStr=getReqBody(operInfo);
		SendUtil.send(resStr);
	}

	private String getReqBody(OperInfo info){
		String reqStr="<1111>SHSNC_APP:{{OPER_TIME}}@@{{SESSION_ID}}@@{{OPER_ACCT}}@@{{USER_NAME}}@@{{ROUND_AUDIT}}@@{{OPER_OBJ}}@@{{SRC_IP}}@@{{DEST_IP}}@@{{SRC_PORT}}@@{{DEST_PORT}}@@{{OPER_TYPE}}@@{{OPER_SUB_TYPE}}@@{{OPER}}@@{{OPER_DESC}}@@{{OPER_RESULT}}@@{{WORK_ORDER}}@@{{AMOUNT}}@@{{FROM_SYS}}@@{{APP_ID}}~~{{APP_NAME}}~~{{MOD_ID}}~~{{MOD_NAME}}";
		FiledInfoUtil filedInfoUtil=new FiledInfoUtil(OperInfo.class);
		Map<String,String> data=filedInfoUtil.deal(info);
		reqStr=translateProcessor.realText(reqStr,data);
		return reqStr;
	}



	private String transformationOperType(Integer requestType){
		if(101==requestType||102==requestType){
			return OperType.AUTH_LOGIN_MANAGE;
		}else{
			return OperType.SERVICE_OPER_MANAGE;
		}
	}
	private String transformationOperSubType(Integer requestType){
		if(101==requestType){
			return OperType.AuthLogin.LOGIN_MANAGE.toString();
		}else if(102==requestType){
			return OperType.AuthLogin.LOGOUT_MANAGE.toString();
		}else{
			return OperType.ServiceOper.KEY_SERVICE_OPER.toString();
		}
	}
}
